package com.swj.common.core.utils;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swj.common.core.model.web.RestResult;
import lombok.extern.slf4j.Slf4j;

import java.util.List;


/**
 * 接口响应数据封装工具类
 * @author zhujindan
 *
 */
@Slf4j
public class ResponseUtil {

	/**
	 * 服务器成功返回用户请求数据的状态码
	 */
	public static Integer SUCCESS_GETS_CODE = 200;

	/**
	 * 服务器成功返回用户请求数据的提示信息
	 */
	public static String SUCCESS_GETS_MSG = "ok";

	/* 新建或修改数据成功返回状态码 */
	public static Integer SUCCESS_POSTS_CODE = 201;

	/* 新建或修改数据成功返回提示信息 */
	public static String SUCCESS_POSTS_MSG = "ok";

	/* 请求进入后台排队（异步请求）成功状态码 */
	public static Integer SUCCESS_ACCEPTED_CODE = 202;

	/* 请求进入后台排队（异步请求）成功提示信息 */
	public static String SUCCESS_ACCEPTED_MSG = "Accepted";

	/* 修改数据成功返回状态码 */
	public static Integer SUCCESS_PUT_CODE = 204;

	/* 修改数据成功返回提示信息 */
	public static String SUCCESS_PUT_MSG = "ok";

	/* 删除数据成功返回状态码 */
	public static Integer SUCCESS_DELETES_CODE = 204;

	/* 删除数据成功返回提示信息 */
	public static String SUCCESS_DELETES_MSG = "ok";

	/* 服务器发生错误状态码 */
	public static Integer ERROR_CODE = 500;

	/* 服务器发生错误提示信息 */
	public static String ERROR_MSG = "error";

	/**
	 * 操作成功，返回没有分页的列表
	 *
	 * @param records 数据记录
	 * @return 带列表的响应对象
	 */
	public static <T> RestResult<List<T>> successWithList(List<T> records) {
		RestResult<List<T>> result = new RestResult<>();
		result.setCode(SUCCESS_GETS_CODE);
		result.setMsg(SUCCESS_GETS_MSG);
		result.setData(records);
		printLog(result);
		return result;
	}

	/**
	 * 操作成功，返回没有分页的列表和自定义的提示
	 *
	 * @param records 数据记录
	 * @return 带列表的响应对象
	 */
	public static <T> RestResult<List<T>> successWithListAndMsg(List<T> records, String msg) {
		RestResult<List<T>> result = new RestResult<>();
		result.setCode(SUCCESS_GETS_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?SUCCESS_GETS_MSG:msg);
		result.setData(records);
		printLog(result);
		return result;
	}

	/**
	 * 操作成功，返回有分页的列表
	 *
	 * @param page 分页对象
	 * @return 列表分页的响应对象
	 */
	public static <T> RestResult<Page<T>> successWithPageList(Page<T> page) {
		RestResult<Page<T>> result = new RestResult<>();
		result.setCode(SUCCESS_GETS_CODE);
		result.setMsg(SUCCESS_GETS_MSG);
		result.setData(page);
		printLog(result);
		return result;
	}

	/**
	 * 操作成功，返回有分页的列表
	 *
	 * @param page 分页对象
	 * @return 列表分页的响应对象
	 */
	public static <T> RestResult<Page<T>> successWithPageListAndMsg(Page<T> page,String msg) {
		RestResult<Page<T>> result = new RestResult<>();
		result.setCode(SUCCESS_GETS_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?SUCCESS_GETS_MSG:msg);
		result.setData(page);
		printLog(result);
		return result;
	}

	/**
	 * 操作成功，返回单个对象
	 *
	 * @param obj 返回的单个对象
	 * @return RestResult对象
	 */
	public static <T> RestResult<T> successWithObj(T obj) {
		RestResult<T> result = new RestResult<>();
		result.setCode(SUCCESS_GETS_CODE);
		result.setMsg(SUCCESS_GETS_MSG);
		result.setData(obj);
		printLog(result);
		return result;
	}

	public static <T> RestResult<T> successWithObjAndMsg(T obj, String msg) {
		RestResult<T> result = new RestResult<>();
		result.setCode(SUCCESS_GETS_CODE);
		result.setData(obj);
		result.setMsg(SysStringUtil.isEmpty(msg)?SUCCESS_GETS_MSG:msg);
		printLog(result);
		return result;
	}

    /**
     * 操作成功，只返回结果，没有记录
     * @return
	 */
	public static RestResult success(String msg) {
		RestResult result = new RestResult();
		result.setCode(SUCCESS_GETS_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?SUCCESS_GETS_MSG:msg);
		printLog(result);
		return result;
	}

	/**
	 * 操作成功，只返回结果，没有记录(查询数据)
     * @return 带提示不带data的result
	 */
	public static RestResult successGets(String msg) {
		RestResult result = new RestResult();
		result.setCode(SUCCESS_GETS_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?SUCCESS_GETS_MSG:msg);
		printLog(result);
		return result;
	}

    /**
     * 操作成功，只返回结果，没有记录(新建或修改)
     * @return 带提示不带data的result
	 */
	public static RestResult<?> successPosts(String msg) {
		RestResult<?> result = new RestResult<>();
		result.setCode(SUCCESS_POSTS_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?SUCCESS_GETS_MSG:msg);
		printLog(result);
        return result;
    }

	/**
	 * 操作成功，只返回结果，没有记录(新建或修改)
	 * @return 带提示不带data的result
	 */
	public static RestResult<?> successPut(String msg) {
		RestResult<?> result = new RestResult<>();
		result.setCode(SUCCESS_PUT_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?SUCCESS_GETS_MSG:msg);
		printLog(result);
		return result;
	}
    
    /**
     * 操作成功，只返回结果，没有记录(删除)
     * @return
	 */
	public static RestResult successDelete(String msg) {
		RestResult result = new RestResult();
		result.setCode(SUCCESS_DELETES_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?SUCCESS_GETS_MSG:msg);
		printLog(result);
        return result;
    }

	/**
	 * 操作失败，返回错误代码，提示
	 * @param code 错误代码
	 * @param msg  信息
	 * @return
	 */
	public static RestResult fail(Integer code,String msg) {
		RestResult result = new RestResult();
		result.setMsg(SysStringUtil.isEmpty(msg)?ERROR_MSG:msg);
		result.setCode(SysStringUtil.isNull(code)?ERROR_CODE:code);
		printLog(result);
		return result;
	}


	public static RestResult fail(String msg) {
		RestResult result = new RestResult();
		result.setCode(ERROR_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?ERROR_MSG:msg);
		printLog(result);
		return result;
	}
    
    /**
     * 操作失败，返回结果和错误描述
     * @param msg
     * @return
     */
    public static RestResult failWithError(String msg, String error) {
		RestResult result = new RestResult();
		result.setCode(ERROR_CODE);
		result.setMsg(SysStringUtil.isEmpty(msg)?ERROR_MSG:msg);
		result.setError(SysStringUtil.isEmpty(error)?ERROR_MSG:error);
		printLog(result);
		return result;
	}

	private static void printLog(RestResult result) {
		String jsonString = JSON.toJSONString(result);
		log.info("返回的响应结果为:{}", jsonString);
	}
}
